using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VWBSUM
    {
        public DataRow Convert2WBSUM(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryWBSUM(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["WBSMANDANT"] = " ".PadRight(2 ); 
         dwReturn["WBSKUNDNR"] = " ".PadRight(8 ); 
         dwReturn["WBSWERK"] = " ".PadRight(3 ); 
         dwReturn["WBSABT"] = " ".PadRight(3 ); 
         dwReturn["WBSWAKZ"] = " ".PadRight(1 ); 
         dwReturn["WBSART"] = " ".PadRight(20 ); 
         dwReturn["WBSDESS"] = " ".PadRight(10 ); 
         dwReturn["WBSCOLORIT"] = " ".PadRight(10 ); 
         dwReturn["WBSFARBE"] = " ".PadRight(10 ); 
         dwReturn["WBSBESCHICH"] = " ".PadRight(10 ); 
         dwReturn["WBSZUSTAND"] = " ".PadRight(4 ); 
         dwReturn["WBSVAR"] = " ".PadRight(2 ); 
         dwReturn["WBSZENSUR"] = " ".PadRight(1 ); 
         dwReturn["WBSZENSURKZ"] = " ".PadRight(1 ); 
         dwReturn["WBSLO"] = " ".PadRight(2 ); 
         dwReturn["WBSLB"] = " ".PadRight(2 ); 
         dwReturn["WBSAUFMSL"] = " ".PadRight(2 ); 
         dwReturn["WBSDPART"] = " ".PadRight(11 ); 
         dwReturn["WBSLPGEBNR"] = " ".PadRight(8 ); 
         dwReturn["WBSLAEKAT"] = " ".PadRight(6 ); 
         dwReturn["WBSBRTKAT"] = " ".PadRight(4 ); 
         dwReturn["WBSDART"] = " ".PadRight(20 ); 
         dwReturn["WBSDVAR"] = " ".PadRight(2 ); 
         dwReturn["WBSMTR"] = " ".PadRight(0 ); 
         dwReturn["WBSQM"] = " ".PadRight(0 ); 
         dwReturn["WBSSTK"] = " ".PadRight(0 ); 
         dwReturn["WBSMTRZU"] = " ".PadRight(0 ); 
         dwReturn["WBSQMZU"] = " ".PadRight(0 ); 
         dwReturn["WBSSTKZU"] = " ".PadRight(0 ); 
         dwReturn["WBSDKZ"] = " ".PadRight(1 ); 
         dwReturn["WBSNEUTR"] = " ".PadRight(1 ); 
         dwReturn["WBSSPEZKAT"] = " ".PadRight(1 ); 
         dwReturn["WBSFILLER"] = " ".PadRight(81 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapWBSUM(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveWBSUM(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryWBSUM(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapWBSUM(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertWBSUM(dwLocal, ITXDBVERSION);
            }
            UpdateWBSUM(dwLocal);
        }
        public void InsertWBSUM(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO WBSUM(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryWBSUM(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM WBSUM WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM WBSUM  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "WBSUM";
            Return = Result;
            return Return;
        }
        public void DeleteWBSUM(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM WBSUM   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateWBSUM(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE WBSUM SET "
                    +"WBSMANDANT= :WBSMANDANT,"
                    +"WBSKUNDNR= :WBSKUNDNR,"
                    +"WBSWERK= :WBSWERK,"
                    +"WBSABT= :WBSABT,"
                    +"WBSWAKZ= :WBSWAKZ,"
                    +"WBSART= :WBSART,"
                    +"WBSDESS= :WBSDESS,"
                    +"WBSCOLORIT= :WBSCOLORIT,"
                    +"WBSFARBE= :WBSFARBE,"
                    +"WBSBESCHICH= :WBSBESCHICH,"
                    +"WBSZUSTAND= :WBSZUSTAND,"
                    +"WBSVAR= :WBSVAR,"
                    +"WBSZENSUR= :WBSZENSUR,"
                    +"WBSZENSURKZ= :WBSZENSURKZ,"
                    +"WBSLO= :WBSLO,"
                    +"WBSLB= :WBSLB,"
                    +"WBSAUFMSL= :WBSAUFMSL,"
                    +"WBSDPART= :WBSDPART,"
                    +"WBSLPGEBNR= :WBSLPGEBNR,"
                    +"WBSLAEKAT= :WBSLAEKAT,"
                    +"WBSBRTKAT= :WBSBRTKAT,"
                    +"WBSDART= :WBSDART,"
                    +"WBSDVAR= :WBSDVAR,"
                    +"WBSMTR= :WBSMTR,"
                    +"WBSQM= :WBSQM,"
                    +"WBSSTK= :WBSSTK,"
                    +"WBSMTRZU= :WBSMTRZU,"
                    +"WBSQMZU= :WBSQMZU,"
                    +"WBSSTKZU= :WBSSTKZU,"
                    +"WBSDKZ= :WBSDKZ,"
                    +"WBSNEUTR= :WBSNEUTR,"
                    +"WBSSPEZKAT= :WBSSPEZKAT,"
                    +"WBSFILLER= :WBSFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("WBSMANDANT", OracleType.NVarChar).Value = dwData["WBSMANDANT"].ToString(); 
          or1.Parameters.Add("WBSKUNDNR", OracleType.NVarChar).Value = dwData["WBSKUNDNR"].ToString(); 
          or1.Parameters.Add("WBSWERK", OracleType.NVarChar).Value = dwData["WBSWERK"].ToString(); 
          or1.Parameters.Add("WBSABT", OracleType.NVarChar).Value = dwData["WBSABT"].ToString(); 
          or1.Parameters.Add("WBSWAKZ", OracleType.NVarChar).Value = dwData["WBSWAKZ"].ToString(); 
          or1.Parameters.Add("WBSART", OracleType.NVarChar).Value = dwData["WBSART"].ToString(); 
          or1.Parameters.Add("WBSDESS", OracleType.NVarChar).Value = dwData["WBSDESS"].ToString(); 
          or1.Parameters.Add("WBSCOLORIT", OracleType.NVarChar).Value = dwData["WBSCOLORIT"].ToString(); 
          or1.Parameters.Add("WBSFARBE", OracleType.NVarChar).Value = dwData["WBSFARBE"].ToString(); 
          or1.Parameters.Add("WBSBESCHICH", OracleType.NVarChar).Value = dwData["WBSBESCHICH"].ToString(); 
          or1.Parameters.Add("WBSZUSTAND", OracleType.NVarChar).Value = dwData["WBSZUSTAND"].ToString(); 
          or1.Parameters.Add("WBSVAR", OracleType.NVarChar).Value = dwData["WBSVAR"].ToString(); 
          or1.Parameters.Add("WBSZENSUR", OracleType.NVarChar).Value = dwData["WBSZENSUR"].ToString(); 
          or1.Parameters.Add("WBSZENSURKZ", OracleType.NVarChar).Value = dwData["WBSZENSURKZ"].ToString(); 
          or1.Parameters.Add("WBSLO", OracleType.NVarChar).Value = dwData["WBSLO"].ToString(); 
          or1.Parameters.Add("WBSLB", OracleType.NVarChar).Value = dwData["WBSLB"].ToString(); 
          or1.Parameters.Add("WBSAUFMSL", OracleType.NVarChar).Value = dwData["WBSAUFMSL"].ToString(); 
          or1.Parameters.Add("WBSDPART", OracleType.NVarChar).Value = dwData["WBSDPART"].ToString(); 
          or1.Parameters.Add("WBSLPGEBNR", OracleType.NVarChar).Value = dwData["WBSLPGEBNR"].ToString(); 
          or1.Parameters.Add("WBSLAEKAT", OracleType.NVarChar).Value = dwData["WBSLAEKAT"].ToString(); 
          or1.Parameters.Add("WBSBRTKAT", OracleType.NVarChar).Value = dwData["WBSBRTKAT"].ToString(); 
          or1.Parameters.Add("WBSDART", OracleType.NVarChar).Value = dwData["WBSDART"].ToString(); 
          or1.Parameters.Add("WBSDVAR", OracleType.NVarChar).Value = dwData["WBSDVAR"].ToString(); 
          or1.Parameters.Add("WBSMTR", OracleType.NVarChar).Value = dwData["WBSMTR"].ToString(); 
          or1.Parameters.Add("WBSQM", OracleType.NVarChar).Value = dwData["WBSQM"].ToString(); 
          or1.Parameters.Add("WBSSTK", OracleType.NVarChar).Value = dwData["WBSSTK"].ToString(); 
          or1.Parameters.Add("WBSMTRZU", OracleType.NVarChar).Value = dwData["WBSMTRZU"].ToString(); 
          or1.Parameters.Add("WBSQMZU", OracleType.NVarChar).Value = dwData["WBSQMZU"].ToString(); 
          or1.Parameters.Add("WBSSTKZU", OracleType.NVarChar).Value = dwData["WBSSTKZU"].ToString(); 
          or1.Parameters.Add("WBSDKZ", OracleType.NVarChar).Value = dwData["WBSDKZ"].ToString(); 
          or1.Parameters.Add("WBSNEUTR", OracleType.NVarChar).Value = dwData["WBSNEUTR"].ToString(); 
          or1.Parameters.Add("WBSSPEZKAT", OracleType.NVarChar).Value = dwData["WBSSPEZKAT"].ToString(); 
          or1.Parameters.Add("WBSFILLER", OracleType.NVarChar).Value = dwData["WBSFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
